package com.winit.project.core.edition;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.BAssert;
import com.winit.common.util.DateUtil;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjian on 16/9/6.
 */
public class PackageEditionConfig {

    private static final Logger logger = LoggerFactory.getLogger(PackageEditionConfig.class);
    private static String INFO_FILE_NAME = "info.json";

    private String path;
    private String projectName;
    private String editionNum;

    private String desc;
    private boolean isProtected;
    private String nasNum;
    //版本创建时间 or 最后构建时间
    private String createDate;
    private boolean notTestPass = false;//测试不通过状态   默认为false

    private long svnVersion;

    public PackageEditionConfig(String projectName, String editionNum, boolean loadInfo) throws Exception{
        BAssert.isEmpty("创建版本ProjectEditionConfig,参数存在空值",logger,projectName,editionNum);
        this.projectName = projectName;
        this.editionNum = editionNum;
        this.path = ProjectUtil.getProjectFilePath(projectName)+editionNum;
        if(loadInfo){
            this.loadFromFile();
        }
    }

    public boolean loadFromFile() throws Exception{
        JSONObject object = ProjectUtil.loadFromFile(this.path+"/"+INFO_FILE_NAME);
        this.desc = object.getString("desc");
        this.isProtected = Boolean.parseBoolean(object.getString("isProtected"));
        this.notTestPass = Boolean.parseBoolean(object.getString("notTestPass"));
        this.nasNum = object.getString("nasNum");
        this.svnVersion = object.getLongValue("svnVersion");
        this.createDate = object.getString("createDate");
        return true;
    }

    public boolean saveToFile() throws Exception{
        JSONObject object = new JSONObject();
        object.put("desc",this.desc);
        object.put("isProtected",this.isProtected);
        object.put("notTestPass",this.notTestPass);
        object.put("nasNum",this.nasNum);
        object.put("editionNum",this.editionNum);
        object.put("createDate",this.createDate);
        object.put("svnVersion",this.svnVersion);
        return ProjectUtil.saveToFile(this.path+"/"+INFO_FILE_NAME,object);
    }

    public String getPath(){
        return this.path;
    }

    public String getProjectName() {
        return projectName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getEditionNum() {
        return editionNum;
    }

    public boolean isProtected() {
        return isProtected;
    }

    public void setProtected(boolean aProtected) {
        isProtected = aProtected;
    }

    public String getNasNum() {
        return nasNum;
    }

    public void setNasNum(String nasNum) {
        this.nasNum = nasNum;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate() {
        String createDate = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss");
        this.createDate = createDate;
    }

    public void setNotTestPass(boolean notTestPass) {
        this.notTestPass = notTestPass;
    }

    public boolean isNotTestPass() {
        return notTestPass;
    }

    public long getSvnVersion() {
        return svnVersion;
    }

    public void setSvnVersion(long svnVersion) {
        this.svnVersion = svnVersion;
    }
}
